﻿using Domain.Production_Modules;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Production_Modules.Pulldown;
using ReturnValue;

namespace MES.Server.API.Application.Handler.Production_Modules.Pulldown
{
    public class BatchDelProductionHandler : IRequestHandler<BatchDelProductionCommand, ApiResult<int>>
    {
        private readonly IRepository<ProduceModel> _produceRepo;

        public BatchDelProductionHandler(IRepository<ProduceModel> produceRepo)
        {
            _produceRepo = produceRepo;
        }
        public async Task<ApiResult<int>> Handle(BatchDelProductionCommand request, CancellationToken cancellationToken)
        {
            List<long> idlist = request.IdString.Split(',').Select(x=>Convert.ToInt64(x)).ToList();
            var result = _produceRepo.GetAll().Where(x => idlist.Contains(x.Id));
            foreach (var item in result)
            {
                item.IsDel = true;
            }
            var r= await _produceRepo.UpdateRangeAsync(result.ToList());
            if (r > 0)
            {
                return new ApiResult<int>().Success("批量删除成功",r);
            }else
            {
                return new ApiResult<int>().Fail("批量删除失败");
            }
        }
    }
}
